import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store/index.js';
const Login = () => import('@/views/login/index.vue')
const Home = () => import('@/views/home/index.vue')
const Question = () => import('@/views/question/index.vue')
const Video = () => import('@/views/video/index.vue')
const User = () => import('@/views/user/index.vue')
const Article = () => import('@/views/article/index.vue')
const Profile = () => import('@/views/user/profile.vue')
const Chat = () => import('@/views/user/chat.vue')
const Tabbar = () => import('@/components/app-tabbar.vue')
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    alias: '/home',
    components: {
      default: Home,
      bottom: Tabbar
    }
  },
  {
    path: '/question',
    components: {
      default: Question,
      bottom: Tabbar
    }
  },
  {
    path: '/video',
    components: {
      default: Video,
      bottom: Tabbar
    }
  },
  {
    path: '/user',
    components: {
      default: User,
      bottom: Tabbar,
      meda: {
        needLogin: true
      }
    }
  },
  {
    path: '/article',
    component: Article
  },
  {
    path: '/user/profile',
    component: Profile,
    meda: {
      needLogin: true
    }
  },
  {
    path: '/user/chat',
    component: Chat,
    meda: {
      needLogin: true
    }
  }
]

const router = new VueRouter({
  routes
})

// 白名单写法
const whitePath = ['user', '/user/profile', '/user/chat']

router.beforeEach((to, from, next) => {
  if (store.getters['user/getToken']) {
    if (to.path.toLowerCase() === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    // if (to.path.toLowerCase().startsWith('/user')) {
    // 白名单写法
    // if (whitePath.includes(to.path.toLowerCase())) {
    // 路由元写法
    if (to.meta.needLogin) {
      next('/login')
    } else {
      next()
    }
  }
})

export default router
